/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "gamelib.h"


odAngles	angles_zero(0.0f, 0.0f, 0.0f);


/*
 ==================
 odAngles::ToVectors
 ==================
*/
void odAngles::ToVectors (odVec3 *forward, odVec3 *right, odVec3 *up) const {

	float	sp, sy, sr, cp, cy, cr;

	M_SinCos(DEG2RAD(pitch), &sp, &cp);
	M_SinCos(DEG2RAD(yaw), &sy, &cy);
	M_SinCos(DEG2RAD(roll), &sr, &cr);

	if (forward)
		forward->Set(cp * cy, cp * sy, -sp);

	if (right)
		right->Set(-sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp);

	if (up)
		up->Set(cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp);
}

/*
 ==================
 odAngles::ToMat3
 ==================
*/
odMat3 odAngles::ToMat3 (void) const {

	odMat3	mat;
	float	sp, sy, sr, cp, cy, cr;

	M_SinCos(DEG2RAD(pitch), &sp, &cp);
	M_SinCos(DEG2RAD(yaw), &sy, &cy);
	M_SinCos(DEG2RAD(roll), &sr, &cr);

	mat[0].Set(cp * cy, cp * sy, -sp);
	mat[1].Set(sr * sp * cy + cr * -sy, sr * sp * sy + cr * cy, sr * cp);
	mat[2].Set(cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp);

	return mat;
}

/*
 ==================
 odAngles::ToString

 This is just a convenience function for printing angles
 ==================
*/
const char *odAngles::ToString (int precision) const {

	return Str_FromFloatArray(ToFloatPtr(), 3, precision);
}
